
package com.chongchong.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chongchong.core.boot.ctrl.SysController;
import com.chongchong.core.mp.support.Condition;
import com.chongchong.core.secure.SysUser;
import com.chongchong.core.tool.api.R;
import com.chongchong.core.tool.constant.SysConstant;
import com.chongchong.core.tool.node.INode;
import com.chongchong.core.tool.utils.CacheUtil;
import com.chongchong.core.tool.utils.Func;
import com.chongchong.system.entity.Role;
import com.chongchong.system.service.IRoleService;
import com.chongchong.system.vo.GrantVO;
import com.chongchong.system.vo.RoleVO;
import com.chongchong.system.wrapper.RoleWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.chongchong.core.tool.utils.CacheUtil.SYS_CACHE;

/**
 * 控制器
 *
 * @author
 */
@RestController
@AllArgsConstructor
@RequestMapping("/role")
@Api(value = "角色", tags = "角色")
public class RoleController extends SysController {

	private IRoleService roleService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入role")
	public R<RoleVO> detail(Role role) {
		Role detail = roleService.getOne(Condition.getQueryWrapper(role));
		return R.data(RoleWrapper.build().entityVO(detail));
	}

	/**
	 * 列表
	 */
	@GetMapping("/list")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "roleName", value = "参数名称", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "roleAlias", value = "角色别名", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "roleProperty", value = "角色属性：0：内部用户；１：外部用户,2,管理员等账号", paramType = "query", dataType = "Integer")
	})
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "列表", notes = "传入role")
	public R<List<INode>> list(@ApiIgnore @RequestParam Map<String, Object> role, SysUser sysUser) {
		QueryWrapper<Role> queryWrapper = Condition.getQueryWrapper(role, Role.class);
		queryWrapper.orderByAsc("sort");
//		queryWrapper.orderByDesc("update_time");
		List<Role> list = roleService.list((!sysUser.getTenantId().equals(SysConstant.ADMIN_TENANT_ID)) ? queryWrapper.lambda().eq(Role::getTenantId, sysUser.getTenantId()) : queryWrapper);
		return R.data(RoleWrapper.build().listNodeVO(list));
	}

	/**
	 * 获取角色树形结构
	 */
	@GetMapping("/tree")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "树形结构", notes = "树形结构")
	public R<List<RoleVO>> tree(String tenantId, SysUser sysUser) {
		List<RoleVO> tree = roleService.tree(Func.toStr(tenantId, sysUser.getTenantId()));
		return R.data(tree);
	}

	/**
	 * 根据角色内外属性获取对应角色树形结构
	 */
	@GetMapping("/treeBYRoleProperty")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "根据角色内外属性获取对应角色树形结构", notes = "根据角色内外属性获取对应角色树形结构")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "tenantId", value = "租号", paramType = "query", dataType = "string",example="000000"),
		@ApiImplicitParam(name = "type", value = "角色属性：0：内部用户；１：外部用户 2：超级管理员等,没传值查全部", paramType = "query", dataType = "Integer")
	})
	public R<List<RoleVO>> treeBYRoleProperty(String tenantId, Integer type, SysUser sysUser) {
		List<RoleVO> tree = roleService.treeBYRoleProperty(Func.toStr(tenantId, sysUser.getTenantId()),type);
		return R.data(tree);
	}
	/**
	 * 获取指定角色树形结构
	 */
	@GetMapping("/tree-by-id")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "获取指定角色树形结构", notes = "获取指定角色树形结构")
	public R<List<RoleVO>> treeById(Long roleId, SysUser sysUser) {
		Role role = roleService.getById(roleId);
		List<RoleVO> tree = roleService.tree(Func.notNull(role) ? role.getTenantId() : sysUser.getTenantId(),roleId);
		return R.data(tree);
	}
	/**
	 * 获取指定角色树形结构
	 */
	@GetMapping("/tree-by-roleId")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "获取当前登录用户角色树形结构", notes = "获取当前登录用户角色树形结构")
	public R<List<RoleVO>> treeById(SysUser sysUser) {
		Role role = roleService.getById(sysUser.getRoleId());
		//role.setId(1557278935930925058L);
		List<RoleVO> tree = roleService.treeChildren(sysUser.getRoleId());
		//List<RoleVO> tree = roleService.tree(Func.notNull(role) ? role.getTenantId() : sysUser.getTenantId(),Long.valueOf(sysUser.getRoleId()));
		return R.data(tree);
	}
	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "新增或修改", notes = "传入role")
	public R submit(@Valid @RequestBody Role role, SysUser user) {
		CacheUtil.clear(SYS_CACHE);
		if (Func.isEmpty(role.getId())) {
			role.setTenantId(user.getTenantId());
		}
		role.setUpdateTime(new Date());
		return R.status(roleService.saveOrUpdate(role));
	}

	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		CacheUtil.clear(SYS_CACHE);
		return R.status(roleService.removeByIds(Func.toLongList(ids)));
	}

	/**
	 * 设置菜单权限
	 */
	@PostMapping("/grant")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "权限设置", notes = "传入roleId集合以及menuId集合")
	public R grant(@RequestBody GrantVO grantVO) {
		CacheUtil.clear(SYS_CACHE);
		boolean temp = roleService.grant(grantVO.getRoleIds(), grantVO.getMenuIds(), grantVO.getDataScopeIds());
		return R.status(temp);
	}

	/**
	 * 无层级拉平角色列表
	 */
	@GetMapping("/listNoPage")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "roleName", value = "角色名", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "roleAlias", value = "角色别名", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "roleProperty", value = "角色属性：0：内部用户；１：外部用户,2,管理员等账号", paramType = "query", dataType = "Integer")
	})
	@ApiOperationSupport(order = 10)
	@ApiOperation(value = "无层级拉平角色列表", notes = "传入role")
	public R<List<RoleVO>> listNoPage(@ApiIgnore @RequestParam Map<String, Object> role, SysUser sysUser) {
		QueryWrapper<Role> queryWrapper = Condition.getQueryWrapper(role, Role.class);
		queryWrapper.eq("is_deleted",0);
		queryWrapper.orderByDesc("update_time");
		List<Role> list = roleService.list((!sysUser.getTenantId().equals(SysConstant.ADMIN_TENANT_ID)) ? queryWrapper.lambda().eq(Role::getTenantId, sysUser.getTenantId()) : queryWrapper);
		return R.data(RoleWrapper.build().listVO(list));
	}
}
